//
//  TopicTableViewCell.m
//  XiangMuB
//
//  Created by 向修根 on 16/8/13.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "TopicTableViewCell.h"
#import "TopocModel.h"
#import <SDWebImage/UIImageView+WebCache.h>

#import "DataBaseHandle.h"
#import "MBProgressHUD.h"

@interface TopicTableViewCell ()
@property (strong,nonatomic) NSString *topicID;

@property (strong,nonatomic) MBProgressHUD *HUD;



@end

@implementation TopicTableViewCell




- (void)layoutSubviews{
    [super layoutSubviews];
    self.name.text = self.model.name;
    self.classification.text = self.model.classification;
    NSString *str = [NSString stringWithFormat:@"%@",self.model.concernCount];
    self.concernCount.text = [str stringByAppendingString:@"关注"];
    
    NSString *str1 = [NSString stringWithFormat:@"%@",self.model.talkCount];
    self.talkCount.text = [str1 stringByAppendingString:@"讨论"];
    NSArray *array = self.model.talkContent;
    NSDictionary *dic = array[0];
    NSDictionary *dic1 = array[1];
    self.OneHeadLable.text = dic[@"content"];
    self.twoHraderLabel.text = dic1[@"content"];
    
    [self.OneHeadImage sd_setImageWithURL:[NSURL URLWithString:dic[@"userHeadPicUrl"]] placeholderImage:nil];
    [self.twoHeadImage sd_setImageWithURL:[NSURL URLWithString:dic1[@"userHeadPicUrl"]] placeholderImage:nil];
    
    self.OneHeadImage.layer.cornerRadius = self.OneHeadImage.frame.size.width / 2;
    self.OneHeadImage.layer.masksToBounds = YES;
    
    self.twoHeadImage.layer.cornerRadius = self.twoHeadImage.frame.size.width / 2;
    self.twoHeadImage.layer.masksToBounds = YES;
    
    //    弧度
    self.cellView.layer.cornerRadius = self.cellView.frame.size.width /50;
    self.cellView.layer.masksToBounds = YES;
    //   cell边框
    self.cellView.layer.borderWidth = 2;
    self.cellView.layer.borderColor = [[UIColor colorWithRed:192.0f/255.0f  green:192.0f/255.0f  blue:192.0f/255.0f  alpha:0.7]CGColor];
    [[DataBaseHandle shareDataBaseHandle]openDatabase];
    NSArray *array1 = [[DataBaseHandle shareDataBaseHandle]select];
   
    for (NSDictionary *dic in array1) {
         NSString *str3 = dic[@"name"];
        if ([str3 isEqualToString:self.name.text]) {
            NSLog(@"str3 %@",str3);
            NSLog(@"%@",self.name.text);
            [self.fouceButton setTitle:@"已关注" forState:(UIControlStateNormal)];
            [self.fouceButton setTintColor:[UIColor blackColor]];
            self.fouceButton.backgroundColor = [UIColor whiteColor];
        }
        


        
    }


   
//    NSLog(@"%@",self.fouceButton.titleLabel.text);

    
}

- (IBAction)fouces:(UIButton *)sender {
    
    if ([self.fouceButton.titleLabel.text isEqualToString:@"+关注"]) {
        
        [self.fouceButton setTitle:@"已关注" forState:(UIControlStateNormal)];
                [self.fouceButton setTintColor:[UIColor blackColor]];
                self.fouceButton.backgroundColor = [UIColor whiteColor];
        [[DataBaseHandle shareDataBaseHandle]openDatabase];
        [[DataBaseHandle shareDataBaseHandle]CreateTable];
        [[DataBaseHandle shareDataBaseHandle]insterTableWithModel:self.model];
        [[DataBaseHandle shareDataBaseHandle]closeDatabase];
        
        self.HUD = [[MBProgressHUD alloc]initWithView:self.cellView];
        [self.cellView addSubview:self.HUD];
        self.HUD.label.text = @"收藏成功";
        self.HUD.mode = MBProgressHUDModeText;
        
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            sleep(2);
        } completionBlock:^{
            [self.HUD removeFromSuperview];
            self.HUD = nil;
        }];
        
    }else{
        [[DataBaseHandle shareDataBaseHandle]openDatabase];
        [[DataBaseHandle shareDataBaseHandle]deleteWithModle:self.model];
        [[DataBaseHandle shareDataBaseHandle]closeDatabase];
        [self.fouceButton setTitle:@"+关注" forState:(UIControlStateNormal)];
        self.fouceButton.backgroundColor = [UIColor redColor];
        
        self.HUD = [[MBProgressHUD alloc]initWithView:self.cellView];
        [self.cellView addSubview:self.HUD];
        self.HUD.label.text = @"取消收藏";
        self.HUD.mode = MBProgressHUDModeText;
        
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            sleep(2);
        } completionBlock:^{
            [self.HUD removeFromSuperview];
            self.HUD = nil;
        }];
    }
    
    
    
    
    
}



- (void)awakeFromNib {
    [self.name NightWithType:UIViewColorTypeClear];
     [self.OneHeadLable NightWithType:UIViewColorTypeClear];
     [self.twoHraderLabel NightWithType:UIViewColorTypeClear];
     [self.classification NightWithType:UIViewColorTypeClear];
     [self.concernCount NightWithType:UIViewColorTypeClear];
      [self.talkCount NightWithType:UIViewColorTypeClear];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
